package com.itheima.reggie.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * CachingConfigurerSupport自定义缓存读写机制,因为redis保存的数据经过了序列化，所以写数据和读数据有差别。
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    /**
     * RedisConnectionFactory连接工厂
     * @param connectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object, Object> objectObjectRedisTemplate = new RedisTemplate<>();
        //默认的key序列化器为：jdkSerializationRedisSerializer.
        objectObjectRedisTemplate.setKeySerializer(new StringRedisSerializer());
        objectObjectRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
        objectObjectRedisTemplate.setConnectionFactory(connectionFactory);
        return objectObjectRedisTemplate;
    }
}
